﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Billing.WindowsBase.CustomControls
{
    [ToolboxItem(true)]
    [DesignerAttribute(typeof(sbLBLTextBoxDesigner), typeof(System.ComponentModel.Design.IDesigner))]
    [DefaultEvent("Initialized")]
    [ToolboxBitmap(typeof(System.Windows.Forms.TextBox))]
    public partial class sbLBLTextBox : BaseControl
    {
        public sbLBLTextBox()
        {
            InitializeComponent();
        }

        public event BootonClickHandler GoBootonClick;
        public event BootonClickHandler DeleteBootonClick;
        public event BootonClickHandler ChooseBootonClick;
        public event BootonClickHandler InfoBootonClick;
        public event InitializedHandler Initialized;

        public event KeyEventHandler TextBoxKeyUp;

        public void Initialize()
        {
            if (this.Initialized != null)
                this.Initialized(this);
        }

        #region Public Properties

        private IBaseObject baseObject;
        public IBaseObject BaseObject
        {
            get { return baseObject; }
            set
            {
                baseObject = value;
                if (baseObject == null)
                {
                    this.txt.Text = string.Empty;
                }
                else
                {
                    this.txt.Text = value.Name;
                }
            }
        }

        [Category("Custom")]
        public Size NameSize
        {
            get { return this.lblName.Size; }
            set { this.lblName.Size = value; }
        }
        [Category("Custom")]
        public string NameText
        {
            get { return this.lblName.Text; }
            set { this.lblName.Text = value; }
        }
        [Category("Custom")]
        public bool ReadOnly
        {
            get { return this.txt.ReadOnly; }
            set { this.txt.ReadOnly = value; }
        }
        [Category("Custom")]
        public char PasswordChar
        {
            get { return this.txt.PasswordChar; }
            set { this.txt.PasswordChar = value; }
        }
        [Category("Custom")]
        public string TextBoxText
        {
            get { return this.txt.Text; }
            set { this.txt.Text = value; }
        }

        [Category("Custom")]
        public bool AllowClear
        {
            get { return this.getButtonVisible("clear"); }
            set { this.setButtonVisible("clear", value); }
        }
        [Category("Custom")]
        public bool AllowGo
        {
            get { return this.getButtonVisible("go"); }
            set { this.setButtonVisible("go", value); }
        }
        [Category("Custom")]
        public bool AllowCoose
        {
            get { return this.getButtonVisible("choose"); }
            set { this.setButtonVisible("choose", value); }
        }
        [Category("Custom")]
        public bool AllowInfo
        {
            get { return this.getButtonVisible("Info"); }
            set { this.setButtonVisible("Info", value); }
        }


        [Bindable(true), Browsable(true)]
        public object Value
        {
            get { return this.txt.Value; }
            set { this.txt.Value = value; }
        }
        [Bindable(true), Browsable(true)]
        public new string Text
        {
            get { return this.txt.Text; }
            set { this.txt.Text = value; }
        }
        [Category("Custom")]
        public bool ShowOverflowIndicator
        {
            get { return this.txt.ShowOverflowIndicator; }
            set { this.txt.ShowOverflowIndicator = value; }
        }
        #endregion

        private void txt_KeyUp(object sender, KeyEventArgs e)
        {
            if (this.TextBoxKeyUp != null)
                this.TextBoxKeyUp(sender, e);

            //if (e.KeyCode == Keys.Enter)
            //    if (this.EnterBootonClick != null)
            //        this.EnterBootonClick();
        }


        private void setButtonVisible(string key, bool value)
        {
            this.txt.ButtonsRight[key].Visible = value;
        }

        private bool getButtonVisible(string key)
        {
            return this.txt.ButtonsRight[key].Visible;
        }

        private void txt_EditorButtonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            switch (e.Button.Key.ToLower())
            {
                case "go":
                    if (this.GoBootonClick != null)
                        this.GoBootonClick(this, e);
                    break;
                case "clear":
                    {
                        if (this.DeleteBootonClick != null)
                            this.DeleteBootonClick(this, e);
                        this.txt.Clear();
                    }
                    break;
                case "choose":
                    if (this.ChooseBootonClick != null)
                    {
                        this.ChooseBootonClick(this, e);
                    }
                    break;
                case "info":
                    if (this.InfoBootonClick != null)
                        this.InfoBootonClick(this, e);
                    break;
                default:
                    break;
            }
        }

        #region Label
        [Category("Custom")]
        public override string LabelText
        {
            get { return this.lblName.Text; }
            set
            {
                this.lblName.Text = value;
                if (this.LabelAutoSize)
                {
                    AutoSizeLabel();
                }
            }
        }

        private void AutoSizeLabel()
        {
            System.Drawing.Graphics gr = this.CreateGraphics();
            this.lblName.Size = new Size((int)gr.MeasureString(this.lblName.Text, this.lblName.Font).Width + 8, this.lblName.Height);
        }
        [Category("Custom")]
        public override bool LabelAutoSize
        {
            get
            {
                return base.LabelAutoSize;
            }
            set
            {
                base.LabelAutoSize = value;
                if (this.LabelAutoSize)
                {
                    AutoSizeLabel();
                }
            }
        }
        [Category("Custom")]
        public override Size LabelSize
        {
            get { return this.lblName.Size; }
            set { this.lblName.Size = value; }
        }
        [Category("Custom")]
        public override bool LabelVisible
        {
            get { return this.lblName.Visible; }
            set { this.lblName.Visible = value; }
        }

        public override Color LabelBorderColor
        {
            get { return this.lblName.Appearance.BorderColor; }
            set { this.lblName.Appearance.BorderColor = value; }
        }
        #endregion

        #region Editor
        [Category("Custom")]
        public override string EditorText
        {
            get { return this.txt.Text; }
            set { this.txt.Text = value; }
        }
        [Category("Custom")]
        public override Size EditorSize
        {
            get { return this.txt.Size; }
            set { this.txt.Size = value; }
        }
        [Category("Custom")]
        public override bool EditorVisible
        {
            get { return this.txt.Visible; }
            set { this.txt.Visible = value; }
        }
        [Category("Custom")]
        public override object EditorSource
        {
            get { return null; }
            set { ;}
        }
        [Category("Custom")]
        public override object EditorValue
        {
            get { return this.txt.Text; }
            set { this.txt.Text = value.ToString(); }
        }
        [Category("Custom")]
        public override bool EditorIsReadOnly
        {
            get { return this.txt.ReadOnly; }
            set { this.txt.ReadOnly = value; }
        }
        #endregion
    }

    #region "Designer
    public class sbLBLTextBoxDesigner : System.Windows.Forms.Design.ControlDesigner
    {
        public override System.Windows.Forms.Design.SelectionRules SelectionRules
        {
            get
            {
                System.Windows.Forms.Design.SelectionRules i = System.Windows.Forms.Design.SelectionRules.RightSizeable |
                    System.Windows.Forms.Design.SelectionRules.Visible |
                    System.Windows.Forms.Design.SelectionRules.LeftSizeable |
                    System.Windows.Forms.Design.SelectionRules.Moveable;

                return i;
            }
        }
    }
    #endregion
}
